package com.itheima.servlet;

import com.itheima.domain.Emp;
import com.itheima.service.EmpService;
import com.itheima.service.impl.EmpServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/EmpServlet")
public class EmpServlet extends HttpServlet {
           private EmpService empService= new EmpServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取所有的请求参数

        Map<String, String[]> map = request.getParameterMap();
        //创建实体类对象用于存储所有的请求参数
        Emp emp = new Emp();
        try {
            //封装请求参数到emp对象中
            BeanUtils.populate(emp,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用service完成添加操作
        empService.add(emp);
            //重定向到查询操作
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
